<template>
  <div class="InventoryAlertPanel">
    <div class="InventoryAlertPanel-title">库存告警</div>
    <div class="InventoryAlertPanel-empty" v-if="!loading && !alerts.length">暂无低库存商品</div>
    <div class="InventoryAlertPanel-cards" v-else>
      <div class="InventoryAlertPanel-card" v-for="(a,i) in alerts" :key="i">
        <div class="InventoryAlertPanel-card-label" :title="a.name">{{ a.name }}</div>
        <div class="InventoryAlertPanel-card-value">{{ a.value }}</div>
      </div>
    </div>
  </div>
</template>
<script setup>
import {ref, onMounted} from 'vue'
import {getInventoryWarning} from '../../../api/systemApi.js'

const alerts = ref([]) // { name, quantity }
const loading = ref(false)

const loadWarnings = async () => {
  loading.value = true
  try {
    const res = await getInventoryWarning()
    if (Array.isArray(res)) {
      alerts.value = res.map(r => {
        const inv = Number(r.inventory ?? 0)
        const unit = r.unit ? String(r.unit) : ''
        return {
          name: r.name ?? '-',
            // 显示格式：数值(两位小数) + 空格 + 单位（若有）
          value: `${inv.toFixed(2)}${unit ? ' ' + unit : ''}`
        }
      })
    } else {
      alerts.value = []
    }
  } catch (e) {
    alerts.value = []
  } finally {
    loading.value = false
  }
}

onMounted(loadWarnings)
</script>
<style scoped lang="scss">
.InventoryAlertPanel {
  display: flex;
  flex-direction: column;
  padding: 16px;
  background: linear-gradient(180deg, #ffffff 0%, #fafbfc 100%);
  border: 1px solid #e8ebf0;
  border-radius: 12px;
  height: 100%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .04), 0 4px 12px rgba(0, 0, 0, .04);
  transition: box-shadow .2s ease;
  overflow: hidden;

  &:hover {
    box-shadow: 0 4px 10px rgba(0, 0, 0, .08), 0 8px 20px rgba(0, 0, 0, .06);
  }

  &-title {
    font-size: 18px;
    font-weight: 600;
    color: #303133;
    margin-bottom: 12px;
    text-align: left;
  }

  &-empty {
    font-size: 14px;
    color: #909399;
  }

  &-cards {
    display: grid;
    grid-template-columns:repeat(auto-fill, minmax(140px, 1fr));
    gap: 12px;
    align-content: start;
    flex: 1;
  }

  &-card {
    background: #f8f9fb;
    border: 1px solid #eef1f5;
    border-radius: 10px;
    padding: 12px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    gap: 8px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
    transition: background .2s ease, transform .15s ease, box-shadow .2s ease;
    aspect-ratio: 1 / 1; /* 保持方形 */
    overflow: hidden;
    cursor: default;

    &:hover {
      background: #ffffff;
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, .08);
    }

    &-label {
      font-size: 14px;
      color: #606266;
      font-weight: 500;
      /* 去除独占宽度与省略，保持与其他面板一致 */
      white-space: normal;
      overflow: visible;
      text-overflow: initial;
    }
    &-value {
      font-size: 22px;
      font-weight: 600;
      line-height: 1;
      letter-spacing: .5px;
      color: #F56C6C; /* 保持告警红色 */
    }
  }
}
</style>
